﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace CSMF
{
    public partial class registerSmallSinaUser : System.Web.UI.Page
    {

        private UserRegistrationService _registrationService = new UserRegistrationService();
        private AuthenticationService _authenticationService = new AuthenticationService();
        private SinaService _sinaService = new SinaService();
        private UserService _userService = new UserService();

        protected void Page_Load(object sender, EventArgs e)
        {
            var weiboUserId = Request.QueryString["weiboUser"];
            weiboid.Value = weiboUserId;
        }

        protected void Reg_Click(object sender, EventArgs e)
        {
            msgLbl.Text=string.Empty;
            if (string.IsNullOrEmpty(UserLogoUrl.Value))
            {
                msgLbl.Text = "请上传头像";
                msgLbl.Visible = true;
                return;
            }

            var user = GetGuestCustomer();


            var request = new UserRegistrationRequest()
            {
                Customer = user,
                Email = this.EmailText.Text,
                Username = this.UserNameText.Text,
                IsApproved = true,
                Password = "csmffmsc",
                UserLogo = UserLogoService.GetUserLogo(this.UserLogoUrl.Value)
            };
            var response = _registrationService.RegisterCustomer(request);


            if (response.Success)
            {
                _userService.BindSinaUser(user.Id, weiboid.Value);

                _authenticationService.SignIn(request.Customer, true);
                msgLbl.Text = "注册成功";
                inputForm.Visible = false;
            }
            else
            {
                var error = response.Errors;
                msgLbl.Text = "注册失败: " + error[0];
                inputForm.Visible = true;
            }

        }

        protected void btnUp_Click(object sender, EventArgs e)
        {
            if (this.fuInsertLogo.PostedFile.FileName != "")
            {
                var picUrl = UserLogoService.FileUploads(fuInsertLogo, "..\\..\\UpLoad\\UserLogo\\");
                lblIMessage.Text = "图片上传成功！";
                this.imgInsertLogo.ImageUrl = picUrl;
                this.UserLogoUrl.Value = picUrl;
            }
            else
            {
                lblIMessage.Text = "请选择要上传的图片！";
            }
        }


        private static UserInfo GetGuestCustomer()
        {
            var _workContext = new WebWorkContext();
            var _authenticationService = new AuthenticationService();
            var userService = new UserService();

            if (_workContext.CurrentUser.IsRegistered())
            {
                //Already registered customer. 
                _authenticationService.SignOut();

                //Save a new record
                userService.InsertGuestCustomer();
            }

            var user = _workContext.CurrentUser;
            return user;
        }
    }
}